﻿#include "MyGDIPlus.h"
#include "MyCImg.h"
#include "MyCairo.h"

#include "MyLine.h"
#include "MyRectangle.h"
#include "MyEllipse.h"
#include "MyTriangle.h"
#include "MyLozenge.h"
#include "MyEffect.h"

#include "pugixml.hpp"
using namespace pugi;

void main()
{	
	HWND console =  GetConsoleWindow();
	MyGraphic* graphic = new MyGDIPlus(console);

	//Các màu sắc tạo sẵn
	char color[] = {125, 0, 255};
	char colorFill[] = {150, 255, 128};	
	char shadow[] = {255, 0, 0};
	char highlight[] = {200, 200, 200};
	char border[] = {255, 255, 0};

	//Vẽ hình chữ nhật và hiệu ứng shadow
	MyShape* rect = new MyRectangle(color, colorFill, 8.5F, 50, 50, 200, 100);	
	rect->SetMyGraphic(graphic);
	MyEffect* shadowEffect = new MyShadow(shadow, 15.0F);
	shadowEffect->SetMyGraphic(graphic);
	shadowEffect->SetShape(rect);
	shadowEffect->Draw();

	//Vẽ hình ellipse và hiệu ứng highlight
	MyShape* ellipse = new MyEllipse(color, colorFill, 8.0F, 320, 50, 200, 100);
	ellipse->SetMyGraphic(graphic);
	MyEffect* highlightEffect = new MyHighlight(highlight, 10.0F);
	highlightEffect->SetMyGraphic(graphic);
	highlightEffect->SetShape(ellipse);
	highlightEffect->Draw();

	//Vẽ hình thoi và hiệu ứng border
	MyShape* lozenge = new MyLozenge(color, NULL, 2.5F, 180, 180, 200, 100);
	lozenge->SetMyGraphic(graphic);
	MyEffect* borderEffect = new MyBorder(border, 10.0F);
	borderEffect->SetMyGraphic(graphic);
	borderEffect->SetShape(lozenge);
	borderEffect->Draw();

	delete rect;
	delete ellipse;
	delete lozenge;
	delete graphic;
}

vector<MyShape*> MyShape::Parse(string filename) {
	vector<MyShape*> shapes;
	
	pugi::xml_document doc;
	std::ifstream stream(filename);
	pugi::xml_parse_result result = doc.load(stream);
	for (pugi::xml_node shape = doc.child("svg").first_child(); shape; shape = shape.next_sibling())
	{
		string type = shape.name();
		if(type == "line") {
			int x1 = shape.attribute("x1").as_int();
			int y1 = shape.attribute("y1").as_int();
			int x2 = shape.attribute("x2").as_int();
			int y2 = shape.attribute("y2").as_int();
			char* stroke = MyFunction::ParseColor(shape.attribute("stroke").value());
			float strokeWidth = shape.attribute("stroke-width").as_float();
			
			MyShape* shape = new MyLine(stroke, strokeWidth, x1, y1, x2, y2);
			shapes.push_back(shape);
			//cout << x1 << endl << y1 << endl << x2 << endl << y2 << endl << stroke << endl;
		}
		else if(type == "rect") {
			int x = shape.attribute("x").as_int();
			int y = shape.attribute("y").as_int();
			int width = shape.attribute("width").as_int();
			int height = shape.attribute("height").as_int();			
			char* stroke = MyFunction::ParseColor(shape.attribute("stroke").value());
			char* fill = MyFunction::ParseColor(shape.attribute("fill").value());
			float strokeWidth = shape.attribute("stroke-width").as_float();
			
			MyShape* shape = new MyRectangle(stroke, fill, strokeWidth, x, y, width, height);
			shapes.push_back(shape);
			//cout << x << endl << y << endl << width << endl << height << endl << stroke << endl << strokeWidth << endl << fill << endl ;
		}
		else if(type == "ellipse") {
			int cx = shape.attribute("cx").as_int();
			int cy = shape.attribute("cy").as_int();
			int rx = shape.attribute("rx").as_int();
			int ry = shape.attribute("ry").as_int();
			char* stroke = MyFunction::ParseColor(shape.attribute("stroke").value());
			char* fill = MyFunction::ParseColor(shape.attribute("fill").value());
			float strokeWidth = shape.attribute("stroke-width").as_float();
			
			MyShape* shape = new MyEllipse(stroke, fill, strokeWidth, cx, cy, rx, ry);
			shapes.push_back(shape);
			//cout << cx << endl << cy << endl << rx << endl << ry << endl << stroke << endl << strokeWidth << endl << fill << endl ;
		}		
	}
	return shapes;
}